package sz.szsmk.citizencard.ui.newversion.fragment.adapter

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import okhttp3.Callback

import com.bumptech.glide.load.engine.DiskCacheStrategy
import kotlinx.android.synthetic.main.layout_homeusermodule.view.*
import sz.szsmk.citizencard.R
import sz.szsmk.citizencard.model.network.bean.response.QueryAllFunctionNewRes
import sz.szsmk.citizencard.model.network.bean.response.QueryAllFunctionNewRes.ClassListBean
import sz.szsmk.citizencard.util.SecureSharedPreferences

/**
 *  author : hyt
 *  date : 2020/8/13
 *  description :
 */
class HomeUserAppModuleAdapter(var context: Context? = null)
    : RecyclerView.Adapter<HomeUserAppModuleAdapter.UserAppModuleHolder>(){

    var homeUserAppList =
            mutableListOf<QueryAllFunctionNewRes.ClassListBean.FunctionListBean>()

    override fun getItemCount() = homeUserAppList.size


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) :UserAppModuleHolder{
        return UserAppModuleHolder(LayoutInflater.from(context)
                .inflate(R.layout.layout_homeusermodule, parent, false))
    }

    override fun onBindViewHolder(holder: UserAppModuleHolder, position: Int) {
        var bean = homeUserAppList[position]
        if(bean.icon_url=="1"){
            Glide.with(context).load(R.mipmap.icon_t10)
                    .skipMemoryCache(true)
                    .diskCacheStrategy(DiskCacheStrategy.NONE) // 不使用磁盘缓存
                    .into(holder.itemView.iv_img)
        }else{
            Glide.with(context).load(bean.icon_url)
                    .skipMemoryCache(true)
                    .diskCacheStrategy(DiskCacheStrategy.NONE) // 不使用磁盘缓存
                    .into(holder.itemView.iv_img)
        }

        holder.itemView.tv_title.text = bean.function_name

        holder.itemView.setOnClickListener {
            val url = bean.link_url
            if (bean.icon_url == "1") {
                //跳转全部
                    moreInterface?.onClick(holder.view, position)
            } else {
                if (bean.function_name.equals("教育E卡通"))
                    baseInterface?.onClick(holder.view, url, null, bean.need_header, bean.function_id.toString() + "", bean)
                else
                    baseInterface?.onClick(holder.view, url, bean.sys_label, bean.need_header, bean.function_id.toString() + "", bean)
            }
        }

    }

    fun addData(homeUserAppList: MutableList<QueryAllFunctionNewRes.ClassListBean.FunctionListBean>) {
        this.homeUserAppList.clear()
        this.homeUserAppList.addAll(homeUserAppList)
        notifyDataSetChanged()
    }

    class UserAppModuleHolder(var view: View) : RecyclerView.ViewHolder(view)

    var moreInterface:MoreInterface?=null

    interface MoreInterface {
        fun onClick(view: View?, position: Int)
    }

    var baseInterface:BaseInterface?=null

    interface BaseInterface {
        fun onClick(view: View?, url: String?, syslabl: String?, need_header: Int, s: String?, bean: ClassListBean.FunctionListBean?)
    }
}